package com.cmy.b_claculator.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.cmy.b_claculator.db.dao.WorkDateDao
import com.cmy.b_claculator.db.entity.WorkDataEntity
import com.cmy.b_claculator.db.entity.WorkEntity
import com.cmy.b_claculator.db.entity.WorkSpeciesEntity
import com.cmy.b_claculator.db.entity.WorkUnitEntity


/**
 * 计算器初步类
 */
@Database(
    entities = [WorkEntity::class, WorkDataEntity::class, WorkSpeciesEntity::class, WorkUnitEntity::class],
    version = 1, exportSchema = false
)
abstract class ClaculatorDatabase : RoomDatabase() {

        abstract val workDao: WorkDateDao

        companion object {
           private const val DB_NAME = "cmy.db"
            private var INSTANCE: ClaculatorDatabase? = null

            fun getInstance(context: Context):ClaculatorDatabase = INSTANCE ?: synchronized(this){
                INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
            }

           private fun buildDatabase(context: Context): ClaculatorDatabase = Room.databaseBuilder(
                context,
                ClaculatorDatabase::class.java,
                DB_NAME)
                .allowMainThreadQueries()
                .build()

        }





}